package com.woniuxy.demo.epai.service.impl;

import com.woniuxy.demo.epai.controller.form.MypageForm;
import com.woniuxy.demo.epai.controller.form.ProductForm;
import com.woniuxy.demo.epai.dao.ClassifyMapper;
import com.woniuxy.demo.epai.dao.ProductMapper;
import com.woniuxy.demo.epai.dao.ProductRecordMapper;
import com.woniuxy.demo.epai.model.Classify;
import com.woniuxy.demo.epai.model.Product;
import com.woniuxy.demo.epai.model.ProductRecord;
import com.woniuxy.demo.epai.service.IProductService;
import com.woniuxy.demo.epai.vo.ProductVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Console;
import java.util.List;

/**
 * <p>
 * 拍品表 服务实现类
 * </p>
 *
 * @author epai
 * @since 2021-04-12
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Resource
    private ClassifyMapper classifyMapper;

    @Resource
    private ProductRecordMapper productRecordMapper;

    //增加条件查询缓存
    @Override
    @Cacheable(value="Term_product",key = "'Term_'+#productForm+#mypageForm.pageindex")
    public MypageForm<Product> queryProductByTerm(MypageForm mypageForm, ProductForm productForm) {
        int pageindex1 = (mypageForm.getPageindex()-1)*mypageForm.getPagesize();
        log.info("条件{}",productForm);
        //获得集合
        if ("全部".equals(productForm.getClassify())){
            productForm.setClassify(null);
        }
        if ("全部".equals(productForm.getP_list())){
            productForm.setP_list(null);
        }
        if ("全部".equals(productForm.getS_list())){
            productForm.setS_list(null);
        }
        int size = mypageForm.getPagesize()+pageindex1;
        log.info("第{}页，到{}",pageindex1,size);

        List<ProductVo> list = productMapper.conditionFindProduct(pageindex1, mypageForm.getPagesize(), productForm.getClassify(),
                productForm.getP_list(), productForm.getS_list());
        //获得总条数
        int count = productMapper.getCount(productForm.getClassify(),productForm.getP_list(), productForm.getS_list());
        //计算总页数

        int pagecount=count%mypageForm.getPagesize()==0?count/mypageForm.getPagesize():count/mypageForm.getPagesize()+1;
        //自己封装mypage
        mypageForm.setList(list);
        mypageForm.setPagecount(pagecount);
        mypageForm.setPageindex(mypageForm.getPageindex());
        log.info("从数据库条件查询到{}",mypageForm);
        return mypageForm;
    }

    /**
     * 根据id查商品
     * @param id
     * @return
     */
    @Override
    public Product queryProductById(Integer id) {
        System.out.println("*************"+id);
        Product product = productMapper.queryProductById(id);
        return product;
    }

    /**
     * 新增商品
     * @param product
     * @return
     */
    @Override
    public int addProduct(Product product) {
        int line = productMapper.addProduct(product);
        return line;
    }
}
